package com.mine.yyb.modules.sys.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.modules.sys.bo.NoticeBO;
import com.mine.yyb.modules.sys.dao.INoticeDao;
import com.mine.yyb.modules.sys.enums.NoticeStatusEnum;
import com.mine.yyb.modules.sys.service.INoticeService;
import com.mine.yyb.modules.sys.vo.NoticeVO;

@Service(value = "iNoticeService")
public class NoticeServiceImpl implements INoticeService
{
    private static Logger logger = LoggerFactory.getLogger(NoticeServiceImpl.class);
    
    @Autowired
    private INoticeDao iNoticeDao;
    
    @Override
    public String saveNotice(NoticeBO noticeBO)
        throws MineException
    {
        try
        {
            logger.info(noticeBO.toString());
            
            int result = 0;
            
            NoticeVO noticeVO = iNoticeDao.queryNotice(new NoticeVO());
            if(noticeVO == null) 
            {
                noticeVO = new NoticeVO();
                noticeVO.setContent(noticeBO.getContent());
                noticeVO.setStatus(NoticeStatusEnum.NOTICE_ANNOUNCED_STATUS.getStatus());
                result = iNoticeDao.save(noticeVO);
            }
            else
            {
                noticeVO = new NoticeVO();
                noticeVO.setContent(noticeBO.getContent());
                noticeVO.setStatus(noticeBO.getStatus());
                result = iNoticeDao.update(noticeVO);
            }
            return result > 0 ? MineErrorCode.SUCCESS.name() : MineErrorCode.ERROR_INTERNAL.name();
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public String updateNotice(NoticeBO noticeBO)
        throws MineException
    {
        try
        {
            logger.info(noticeBO.toString());
            
            NoticeVO noticeVO = new NoticeVO();
            noticeVO.setContent(noticeBO.getContent());
            noticeVO.setStatus(noticeBO.getStatus());
            
            int result = iNoticeDao.update(noticeVO);
            return result > 0 ? MineErrorCode.SUCCESS.name() : MineErrorCode.ERROR_INTERNAL.name();
            
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public String deleteNotice(NoticeBO noticeBO)
        throws MineException
    {
        try
        {
            logger.info(noticeBO.toString());
            
            NoticeVO noticeVO = new NoticeVO();
            noticeVO.setId(noticeBO.getId());
            
            int result = iNoticeDao.delete(noticeVO);
            return result > 0 ? MineErrorCode.SUCCESS.name() : MineErrorCode.ERROR_INTERNAL.name();
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public NoticeVO queryNotice(NoticeBO noticeBO)
        throws MineException
    {
        try
        {
            logger.info(noticeBO.toString());
            
            NoticeVO noticeVO = new NoticeVO();
            noticeVO.setStatus(noticeBO.getStatus());
            
            return iNoticeDao.queryNotice(noticeVO);
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
}
